<!-- 其他出库结算 -->
<template>
  <div class="mod-config">
    <el-button-group style="margin-bottom: 10px;">
      <el-button icon="el-icon-search" type="primary" @click="searchHandle">查询</el-button>
      <el-button icon="el-icon-delete" type="primary" @click="searchReset">清空</el-button>
      <el-button v-if="permissions.eims_takeinventory_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
    </el-button-group>
    <el-form inline label-width="100px" @keyup.enter.native="searchHandle">
      <el-form-item label="单据编号">
        <el-input v-model="dataForm.takeBillId"></el-input>
      </el-form-item>
      <el-form-item label="结算日期">
        <el-row>
          <el-col :span="11">
            <el-date-picker style="width: 100%;" v-model="dataForm.financeTimeStart" value-format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
          </el-col>
          <el-col :span="2" style="text-align: center;">至</el-col>
          <el-col :span="11">
            <el-date-picker style="width: 100%;" v-model="dataForm.financeTimeEnd" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"></el-date-picker>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="发出仓位">
        <el-select v-model="dataForm.outStockId" clearable>
          <el-option v-for="item in stockOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单据日期">
        <el-row>
          <el-col :span="11">
            <el-date-picker style="width: 100%;" v-model="dataForm.createTimeStart" value-format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
          </el-col>
          <el-col :span="2" style="text-align: center;">至</el-col>
          <el-col :span="11">
            <el-date-picker style="width: 100%;" v-model="dataForm.createTimeEnd" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"></el-date-picker>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="审核状态">
        <el-select v-model="dataForm.isAudit" placeholder="请选择" clearable>
          <el-option v-for="item in isAuditOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="结算人">
        <el-input v-model="dataForm.financeUserName"></el-input>
      </el-form-item>
    </el-form>

    <div class="avue-crud">
      <el-table :data="dataList" border v-loading="dataListLoading" :max-height="tableHeight">
        <el-table-column label="操作" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-search" @click="showDetails(scope.row.id)">详情</el-button>
            <el-button v-if="permissions.eims_takeinventory_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)" :disabled="scope.row.inveStatus == '2'">修改</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="takeBillId" label="单据编号" width="190"></el-table-column>
        <el-table-column prop="outStockName" label="发出仓位" minwidth="140"></el-table-column>
        <el-table-column prop="inveType" label="出库类型" width="140" :formatter="inveTypeFormatter"></el-table-column>
        <el-table-column prop="receptionName" label="领用部门" minwidth="140"></el-table-column>
        <el-table-column prop="createTime" label="单据日期" width="140"></el-table-column>
        <el-table-column prop="auditTime" label="出库日期" width="140"></el-table-column>
        <el-table-column prop="financeUserName" label="结算人" width="80"></el-table-column>
        <el-table-column prop="financeTime" label="结算日期" width="80"></el-table-column>
      </el-table>
    </div>
    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100, 200]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next"></el-pagination>
    </div>
    <!-- 弹窗, 详情 -->
    <detail-table v-if="detailsVisible" ref="details" @refreshDataList="getDataList"></detail-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-table v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-table>
  </div>
</template>

<script>
  import { filterParams } from "@/util/tools"
  import { getOtherOutboundSettlementPage } from "@/api/eims/take-inventory/other-outbound"
  import detailTable from "./details"
  import AddTable from "./add"
  import { getStockList } from "@/api/eims/stock"
  import { getDeptInfo } from "@/api/admin/dept"
  import { mapGetters } from "vuex"

  export default {
    data() {
      return {
        printUrl: "",
        dataForm: {
          supplyName: "",
          brandName: "",
        },
        dataList: [],
        stockOptions: [],
        isAuditOptions: [{
            label: "已审核",
            value: 1
          },
          {
            label: "未审核",
            value: 0
          },
        ],
        receptionDeptOptions: [],
        pageIndex: 1,
        pageSize: 200,
        totalPage: 0,
        dataListLoading: false,
        detailsVisible: false,
        supplyVisible: false,
        brandVisible: false,
        addOrUpdateVisible: false,
      };
    },
    created() {
      this.getDataList()
      this.stockList()
      this.getDeptList()
    },
    computed: {
      ...mapGetters(["permissions", "bizData"]),
      tableHeight: function(){
        return window.innerHeight - 260
      }
    },
    components: {
      detailTable,
      AddTable,
    },
    methods: {
      //  查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        let params1 = filterParams(this.dataForm)
        let params2 = {
          current: this.pageIndex,
          size: this.pageSize
        }
        getOtherOutboundSettlementPage(Object.assign(params2, params1)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        this.$confirm("是否确认删除ID为" + id, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(function() {
          return delObj(id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success("删除成功")
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 查看详情
      showDetails(id) {
        this.detailsVisible = true;
        this.$nextTick(() => {
          this.$refs.details.init(id);
        });
      },
      //  获取仓位列表
      stockList() {
        getStockList().then(response => {
          if(response.data.code == '0'){
            this.stockOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      getDeptList() {
        getDeptInfo().then(response => {
          if(response.data.code == '0'){
            this.receptionDeptOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //清空查询列表
      searchReset() {
        for (let key in this.dataForm) {
          this.dataForm[key] = "";
        }
      },
      inveTypeFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case '1':
            return "销售出库";
            break;
          case '2':
            return "领用出库";
            break;
          case '3':
            return "其他出库";
            break;
        }
      }
    }
  }
</script>
